from flask import Blueprint, jsonify
from sqlalchemy import desc

from applications.extensions import db
from applications.models import News

admin_emergency = Blueprint('adminEmergency', __name__, url_prefix='/emergency')


# 查询最新应急演练的状态
@admin_emergency.get('/status')
def get_training_status():
    news = News.query.order_by(desc('occur_time')).limit(1).one()
    return jsonify(code=200, msg="OK", data={'status': news.status, 'alarm_type': news.alarm_type, 'name': news.name,
                                             'content': news.content})


# 开始应急演练训练
@admin_emergency.post('/start')
def start_emergency_training():
    news = News.query.order_by(desc('occur_time')).limit(1).one()
    if news.status == 0:
        news.status = 1
        db.session.commit()
    return jsonify(code=200, msg="OK", data=None)
